package com.hxs.hxsserver.utils;

import org.apache.commons.lang3.RandomStringUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Random;

public class NumberUtil {

    private static final String CHARACTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    private static final int STRING_LENGTH = 6;

    /**
     * 判断是否为16进制数据
     *
     * @param str
     * @return
     */
    public static boolean isHexNumberRex40(String str) {
        String validate = "^0x[A-Fa-f0-9]{40}$";

        return str.matches(validate);

    }

    /**
     * 生成订单号（20位）：时间（精确到毫秒）+3位随机数
     */
    public static synchronized String getOrderNum() {
        //时间（精确到毫秒）
        DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
        String localDate = LocalDateTime.now().format(ofPattern);
        //3位随机数
        String randomNumeric = RandomStringUtils.randomNumeric(3);
        String orderNum = localDate + randomNumeric;
        return orderNum;
    }

    /**
     * 随机几位数字
     * @param digits
     * @return int
     */
    public static int generateRandomNumber(int digits) {
        Random random = new Random();
        int lowerBound = (int) Math.pow(10, digits - 1);
        int upperBound = (int) Math.pow(10, digits) - 1;
        return random.nextInt(upperBound - lowerBound + 1) + lowerBound;
    }
}
